﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Text;
using System.Web.SessionState;

namespace jnagr.Web.sicpet
{
    /// <summary>
    /// GetBPY 的摘要说明
    /// </summary>
    public class GetBPY : IHttpHandler, IRequiresSessionState
    {
        jnagr.BLL.cxinfo bll = new jnagr.BLL.cxinfo();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "delete")
                delete(context);           
            else if (ac == "add")
                add(context);           
            //else if (ac == "modify")
            //    modify(context);
        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string devID = "", StartTime = "", EndTime = "";

                if (null != context.Request.Form["devID"])
                {
                    devID = context.Request.Form["devID"].ToString().Trim();
                }
                else if (null != context.Request.QueryString["devID"])
                {
                    devID = context.Request.QueryString["devID"].ToString().Trim();
                }
                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                else if (null != context.Request.QueryString["StartTime"])
                {
                    StartTime = context.Request.QueryString["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }
                else if (null != context.Request.QueryString["EndTime"])
                {
                    EndTime = context.Request.QueryString["EndTime"].ToString().Trim();
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" updatetime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" updatetime<='{0}' and ", EndTime);
                }
                strWhere.AppendFormat(" devid='{0}'", devID);
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid > 0)
            {
                //string mpic = context.Request.Form["mpic"];
                string infocontent = context.Request.Form["remark"];
                string adddevid = context.Request.Form["adddevid"];

                string savepath = "";
                //if (mpic == "")
                //{
                    HttpFileCollection files = context.Request.Files;
                    if (files.Count == 1)
                    {
                        HttpPostedFile file = files[0];
                        if (file.ContentLength > 0)
                        {
                            //全路径  
                            string FullFullName = file.FileName;
                            //获取图片的名称  
                            String fileName = FullFullName.Substring(FullFullName.LastIndexOf("\\") + 1);
                            string exp = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
                            string filenames = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                            string path = "~/mpics";
                            savepath = "/mpics";
                            if (exp == ".jpg" || exp == ".bmp" || exp == ".gif" || exp == ".png")
                            {
                                file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path) + "\\" + filenames + exp);
                                savepath += "/" + filenames + exp;
                            }
                            else
                            {
                                context.Response.Write("P");//返回给前台页面  
                                context.Response.End();

                            }
                        }
                    }

                //}
                //else
                //{
                //    savepath = mpic;
                //}
                //string uids = context.Request.Form["senduser"];
                //if (uids!=null && uids != "")//发送
                //{
                //    string title = "孢子预警";
                //    string info = infocontent + string.Format("<img src='{0}'></img>", savepath);
                //    jnagr.Model.info modeli = new jnagr.Model.info();
                //    modeli.title = title;
                //    modeli.infotime = DateTime.Now;
                //    modeli.uid = uid;
                //    modeli.infocontent = info;                    
                //    jnagr.BLL.info blli = new jnagr.BLL.info();
                //    int irid=0;
                //    irid=blli.Adds(modeli, uids);
                //    if (mpic!="" && irid > 0)
                //    {
                //        context.Response.Write("T");//返回给前台页面  
                //        context.Response.End();
                //    }
                //}
                //if (mpic == "")
                //{
                    jnagr.Model.cxinfo model = new jnagr.Model.cxinfo();
                    model.uid = uid;
                    model.updatetime = DateTime.Now;
                    model.uppic = savepath;
                    model.devid = adddevid;
                    model.infos = infocontent;
                    int rid = 0;
                    rid = bll.Add(model);
                    if (rid > 0)
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }
                //}
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        //public void modify(HttpContext context)
        //{
        //    context.Response.ContentType = "text/plain";
        //    int uid = Web.Admin.myinfo.getID(context);
        //    if (uid > 0)
        //    {
        //        string title = context.Request.Form["title"];
        //        //string caidstr = context.Request.Form["caid"];
        //        int caid = 0, iid = 0;
        //        //caid = int.Parse(caidstr);
        //        string infocontent = context.Request.Form["infocontent"];
        //        string iidstr = context.Request.Form["iid"];
        //        if (iidstr != "") iid = int.Parse(iidstr);

        //        jnagr.Model.info model = new jnagr.Model.info();
        //        model.title = title;
        //        //model.caid = caid;
        //        model.infocontent = infocontent;
        //        model.iid = iid;
        //        if (bll.Update(model))
        //        {
        //            context.Response.Write("T");//返回给前台页面  
        //            context.Response.End();
        //        }
        //        else
        //        {
        //            context.Response.Write("F");//返回给前台页面  
        //            context.Response.End();
        //        }
        //    }
        //    else
        //    {
        //        context.Response.Write("L");//返回给前台页面  
        //        context.Response.End();
        //    }
        //}
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid>0)
            {
                int cxid = 0;
                //int myid = Web.Admin.myinfo.getID(context);
                string uidstr = context.Request.QueryString["cxid"];
                if (uidstr != "")
                    cxid = int.Parse(uidstr);
                if (bll.DeleteCheck(cxid,uid))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("不能删除别人上报数据！");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}